package cn.offcn.controller;

import cn.offcn.service.UserService;
import cn.offcn.utils.OAResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/logout")
    public void  logout(HttpServletRequest request, HttpServletResponse response)throws Exception{
        HttpSession session=request.getSession();
        if(session!=null)
            session.invalidate();
        response.sendRedirect(request.getContextPath()+"/login.jsp");
    }
    @RequestMapping(value="/checkLogin",method = RequestMethod.POST)
    @ResponseBody
    public OAResult checkLogin(String username, String password, HttpSession session){

       OAResult oaResult= userService.getUserByUname(username,password);
       if(oaResult.getStatus()==200){
           session.setAttribute("activeUser",oaResult.getData());
       }
       return oaResult;
    }
}
